package com.maoyu.cloud.spring.controller;

import com.alibaba.fastjson2.JSON;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class BaseController {
    @Resource
    private MessageSource messageSource;
    public String successJson(String i18nKey, String ...args) {
        Locale locale = LocaleContextHolder.getLocale();
        String msg = messageSource.getMessage(i18nKey,args,locale);
        Map<String, Object> map = new HashMap<>();
        map.put("success",true);
        map.put("msg", msg);
        map.put("code", 200);
        return JSON.toJSONString(map);
    }

    public String failureJson(String i18nKey, Integer returnCode, String ...args) {
        Locale locale = LocaleContextHolder.getLocale();
        String msg = messageSource.getMessage(i18nKey,args,locale);
        Map<String, Object> map = new HashMap<>();
        map.put("success",false);
        map.put("msg", msg);
        map.put("code",returnCode);
        return JSON.toJSONString(map);
    }

}
